package com.example.x6.configurationmaintenance.Activity;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.support.annotation.Nullable;
import android.widget.Toast;
import com.example.x6.configurationmaintenance.DataBase.ServiceDaemon.ServiceDaemonDatabaseOperation;
import com.example.x6.configurationmaintenance.Dialog.YesOrNoDialog;
import com.example.x6.configurationmaintenance.Interface.OnYesOrNoClick;
import com.example.x6.configurationmaintenance.Interface.ServiceDaemonDatabase.OnServiceDaemonDatabaseErgodicListener;
import com.example.x6.configurationmaintenance.R;

public class ServiceDaemonActivity extends PreferenceActivity {
    private final String TAG = getClass().getSimpleName();
    private Preference explain;
    private Handler handler;
    private PackageManager packageManager;
    private ServiceDaemonDatabaseOperation serviceDaemonDatabaseOperation;
    private PreferenceCategory serviceList;

    /* access modifiers changed from: protected */
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.service_daemon);
        this.handler = new Handler();
        ServiceDaemonDatabaseOperation.initializeInstance(this);
        this.serviceDaemonDatabaseOperation = ServiceDaemonDatabaseOperation.getInstance();
        this.serviceList = (PreferenceCategory) findPreference(getResources().getString(R.string.service_list));
        this.packageManager = getPackageManager();
        initView();
    }

    private void initView() {
        this.explain = new Preference(this);
        this.explain.setTitle(getResources().getString(R.string.tips));
        this.explain.setSummary(getResources().getString(R.string.serarching_daemons));
        this.serviceList.addPreference(this.explain);
        this.serviceDaemonDatabaseOperation.setOnServiceDaemonDatabaseErgodicListener(new OnServiceDaemonDatabaseErgodicListener() {
            /* class com.example.x6.configurationmaintenance.Activity.ServiceDaemonActivity.AnonymousClass1 */

            @Override // com.example.x6.configurationmaintenance.Interface.ServiceDaemonDatabase.OnServiceDaemonDatabaseErgodicListener
            public void serviceDaemonDatabaseErgodic(String service) {
                ServiceDaemonActivity.this.syncPreference(service);
            }

            @Override // com.example.x6.configurationmaintenance.Interface.ServiceDaemonDatabase.OnServiceDaemonDatabaseErgodicListener
            public void serviceDaemonDatabaseErgodicFinished(boolean isEmpty) {
                ServiceDaemonActivity.this.dismissExplain();
                if (isEmpty) {
                    ServiceDaemonActivity.this.Toast(ServiceDaemonActivity.this.getResources().getString(R.string.no_daemons));
                }
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void dismissExplain() {
        runOnUiThread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Activity.ServiceDaemonActivity.AnonymousClass2 */

            public void run() {
                ServiceDaemonActivity.this.serviceList.removePreference(ServiceDaemonActivity.this.explain);
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void syncPreference(final String service) {
        runOnUiThread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Activity.ServiceDaemonActivity.AnonymousClass3 */

            public void run() {
                final Preference preference = new Preference(ServiceDaemonActivity.this);
                if (ServiceDaemonActivity.this.getIcon(ServiceDaemonActivity.this.getServicePackagename(service)) != null) {
                    preference.setIcon(ServiceDaemonActivity.this.getIcon(ServiceDaemonActivity.this.getServicePackagename(service)));
                }
                String appName = ServiceDaemonActivity.this.getAppName(ServiceDaemonActivity.this, ServiceDaemonActivity.this.getServicePackagename(service));
                if (appName == null || appName.isEmpty()) {
                    appName = ServiceDaemonActivity.this.getResources().getString(R.string.app);
                } else {
                    preference.setTitle(appName);
                }
                preference.setSummary(service);
                final YesOrNoDialog yesOrNoDialog = new YesOrNoDialog(ServiceDaemonActivity.this);
                yesOrNoDialog.setTitle(ServiceDaemonActivity.this.getResources().getString(R.string.tips));
                yesOrNoDialog.setMessage(ServiceDaemonActivity.this.getResources().getString(R.string.sure_cancel_daemons) + String.valueOf(appName) + " ?");
                yesOrNoDialog.setOnYesOrNoClick(new OnYesOrNoClick() {
                    /* class com.example.x6.configurationmaintenance.Activity.ServiceDaemonActivity.AnonymousClass3.AnonymousClass1 */

                    @Override // com.example.x6.configurationmaintenance.Interface.OnYesOrNoClick
                    public void onYesPressed() {
                        new Thread(new Runnable() {
                            /* class com.example.x6.configurationmaintenance.Activity.ServiceDaemonActivity.AnonymousClass3.AnonymousClass1.AnonymousClass1 */

                            public void run() {
                                if (ServiceDaemonActivity.this.serviceDaemonDatabaseOperation.delete(service) == 0) {
                                    ServiceDaemonActivity.this.handler.post(new Runnable() {
                                        /* class com.example.x6.configurationmaintenance.Activity.ServiceDaemonActivity.AnonymousClass3.AnonymousClass1.AnonymousClass1.AnonymousClass1 */

                                        public void run() {
                                            ServiceDaemonActivity.this.serviceList.removePreference(preference);
                                        }
                                    });
                                    ServiceDaemonActivity.this.Toast(ServiceDaemonActivity.this.getResources().getString(R.string.delete_sucessful));
                                    return;
                                }
                                ServiceDaemonActivity.this.Toast(ServiceDaemonActivity.this.getResources().getString(R.string.delete_failed));
                            }
                        }).start();
                    }

                    @Override // com.example.x6.configurationmaintenance.Interface.OnYesOrNoClick
                    public void onNoPressed() {
                    }
                });
                preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                    /* class com.example.x6.configurationmaintenance.Activity.ServiceDaemonActivity.AnonymousClass3.AnonymousClass2 */

                    public boolean onPreferenceClick(Preference preference) {
                        yesOrNoDialog.showDialog();
                        return true;
                    }
                });
                ServiceDaemonActivity.this.serviceList.addPreference(preference);
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private String getServicePackagename(String service) {
        if (service == null || service.isEmpty()) {
            return null;
        }
        return service.split("/")[0].trim();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private String getAppName(Context context, String packageName) {
        PackageManager pm = context.getPackageManager();
        try {
            return pm.getApplicationLabel(pm.getApplicationInfo(packageName, 128)).toString();
        } catch (PackageManager.NameNotFoundException e) {
            return "";
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private Drawable getIcon(String packageName) {
        if (packageName == null) {
            return null;
        }
        PackageManager packageManager2 = getPackageManager();
        try {
            return packageManager2.getApplicationIcon(packageManager2.getApplicationInfo(packageName, 128));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void Toast(final String toast) {
        if (toast != null && !toast.isEmpty()) {
            runOnUiThread(new Runnable() {
                /* class com.example.x6.configurationmaintenance.Activity.ServiceDaemonActivity.AnonymousClass4 */

                public void run() {
                    Toast.makeText(ServiceDaemonActivity.this, toast, 0).show();
                }
            });
        }
    }
}
